package pCloud.message;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * This represents the data transfered back to the ProxyServer after the access
 * control decision is made (Permit). This is wrapped inside a DataServerReply
 * message, which also contains the decision.
 */
public class CopyOfDatabaseReply extends DatabaseMessage {

	/**
	 * <columnID, column values>
	 */
	HashMap<String, ArrayList> response;
	
	public CopyOfDatabaseReply() {
		this.databaseMsgType = DatabaseMessage.REPLY;
		response = new HashMap<String, ArrayList>();
	}

	/**
	 * This is called to construct the response (ResultSet), because ResultSet
	 * is not readily Serializable
	 */
	public void addColumn(String columnId, ArrayList values) {
		response.put(columnId, values);
	}

	public ArrayList getColumn(String columnId) {
		return response.get(columnId);
	}

	public Set<String> getColumns(){
		return response.keySet();
	
	}
	
	public String toString() {
		String output = "";
		if (response == null)
			return output;

		Object[] keySet = response.keySet().toArray();
		int nKeys = keySet.length;
		for (int i = 0; i < nKeys; i++) {
			output = output + keySet[i] + "\t";
		}
		output += "\n";

		int size = response.get(keySet[0]).size();
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < nKeys; j++) {
				output += response.get(keySet[j]).get(i) + "\t";
			}
			output += "\n";
		}

		return output;
	}
}
